<?php

namespace app\admin\model;

/**
 * 书本上传相关功能
 */
class UploadDownTxt {
    
    //单行 以换行分开
    public function read_txt($fileName) {
        $contents = file_get_contents($fileName);
        //获取文件的编码方式
        $encoding = mb_detect_encoding($contents, array('GB2312', 'GBK', 'UTF-16', 'UCS-2', 'UTF-8', 'BIG5', 'ASCII'));
        $fp = fopen($fileName, "r");
        //以只读的方式打开文件
        $text = "
";
        $num = 0;
        if (!(feof($fp))) {
            $num++;
            $str = trim(fgets($fp));
            if ($encoding != false) {
                //iconv ()将$encoding 转换成“UTF-8”
                $str = iconv($encoding, 'UTF-8', $str);
                if ($str != "" and $str != NULL) {
                    $text = $str;
                }
            } else {
                $str = mb_convert_encoding($str, 'UTF-8', 'Unicode');
                if ($str != "" and $str != NULL) {
                    $text = $str;
                }
            }
        }
        while (!(feof($fp))) {
            $str = '';
            $str = trim(fgets($fp));
            if ($encoding != false) {
                $str = iconv($encoding, 'UTF-8', $str);
                if ($str != "" and $str != NULL) {
                    $text = $text . "
" . $str;
                }
            } else {
                $str = mb_convert_encoding($str, 'UTF-8', 'Unicode');
                if ($str != "" and $str != NULL) {
                    $text = $text . "
" . $str;
                }
            }
        }
        $text .= '
';
        $novels = explode(PHP_EOL, $text);
        //去除空格
        $novels = array_map(function($novel){
            return trim(strip_tags($novel));
        },$novels);
        //去除换行是空的
        $novels = array_filter($novels, function($novel){
            return $novel != "";
        });
        return $novels ;
    }

    //多行以@@分开
    public function read_txt_3($fileName) {
        $contents = file_get_contents($fileName);
        //获取文件的编码方式
        $encoding = mb_detect_encoding($contents, array('GB2312', 'GBK', 'UTF-16', 'UCS-2', 'UTF-8', 'BIG5', 'ASCII'));
        $fp = fopen($fileName, "r");
        //以只读的方式打开文件
        $text = "
";
        $num = 0;
        if (!(feof($fp))) {
            $num++;
            $str = str_replace(PHP_EOL,"",(fgets($fp)));
            if ($encoding != false) {
                //iconv ()将$encoding 转换成“UTF-8”
                $str = iconv($encoding, 'UTF-8', $str);
                if ($str != "" and $str != NULL) {
                    $text = $str;
                }
            } else {
                $str = mb_convert_encoding($str, 'UTF-8', 'Unicode');
                if ($str != "" and $str != NULL) {
                    $text = $str;
                }
            }
        }
        while (!(feof($fp))) {
            $str = '';
            $str = str_replace(PHP_EOL,"",(fgets($fp)));
            if ($encoding != false) {
                $str = iconv($encoding, 'UTF-8', $str);
                if ($str != "" and $str != NULL) {
                    $text = $text . "
" . $str;
                }
            } else {
                $str = mb_convert_encoding($str, 'UTF-8', 'Unicode');
                if ($str != "" and $str != NULL) {
                    $text = $text . "
" . $str;
                }
            }
        }
        $text .= '
';

        $text = str_replace(["@@".PHP_EOL], "@@", $text);
        $novels = explode("@@", $text);
        
        //去除空格
        $novels = array_map(function($novel){
            $novel = strip_tags($novel);
            $novel = str_replace(["&emsp;", "  "],"",  $novel );
            $novel = $novel ? "&emsp;&emsp;".$novel : '';
            $novel = str_replace(["  ", '    '], "&emsp;&emsp;",  $novel );

            $novel = str_replace(PHP_EOL, PHP_EOL."&emsp;&emsp;",  $novel );
            $novel = trim(strip_tags($novel), PHP_EOL);
            $novel = trim(strip_tags($novel), "&emsp;&emsp;");
            $novel = trim(strip_tags($novel), PHP_EOL);
            return  $novel?"&emsp;&emsp;".trim($novel):'';
        },$novels);
        //去除换行是空的
        $novels = array_filter($novels, function($novel){
            return $novel != "";
        });
        // dump($novels);exit;
        return $novels ;
    }

    //下载文案 以换行分开
    public function down_txt($chapter) {
        $filename ="文案下载.txt";
		$encoded_filename = urlencode($filename);    
		$encoded_filename = str_replace("+", "%20", $encoded_filename);  
		header("Content-Type: application/octet-stream");      
		if (preg_match("/MSIE/", $_SERVER['HTTP_USER_AGENT']) ) {
			header('Content-Disposition:  attachment; filename="' . $encoded_filename . '"');      
		} elseif (preg_match("/Firefox/", $_SERVER['HTTP_USER_AGENT'])) {      
			header('Content-Disposition: attachment; filename*="utf8' .  $filename . '"');      
		} else {      
			header('Content-Disposition: attachment; filename="' .  $filename . '"');      
		} 
		if($chapter){
			foreach ($chapter as $k=>$v){
				$chaptername = "";
				$content = $v;
				$content =str_replace(array("<p></p>"),"",$content);
				// $chaptername = $v;
				$content = str_replace(array("<p>","</p>"),array("","\r\n\r\n\t"),$content).PHP_EOL;
                $content = htmlspecialchars_decode($content,ENT_QUOTES );
                $content = str_replace(["&emsp;","  "], "", $content);
				echo strip_tags($content);
			}
		}
        exit();
    }

    //下载文案以@@分开
    public function down_txt3($chapter) {
        $filename ="文案下载.txt";
		$encoded_filename = urlencode($filename);    
		$encoded_filename = str_replace("+", "%20", $encoded_filename);  
		header("Content-Type: application/octet-stream");      
		if (preg_match("/MSIE/", $_SERVER['HTTP_USER_AGENT']) ) {
			header('Content-Disposition:  attachment; filename="' . $encoded_filename . '"');      
		} elseif (preg_match("/Firefox/", $_SERVER['HTTP_USER_AGENT'])) {      
			header('Content-Disposition: attachment; filename*="utf8' .  $filename . '"');      
		} else {      
			header('Content-Disposition: attachment; filename="' .  $filename . '"');      
		} 
        // echo 4;exit;
		if($chapter){
			foreach ($chapter as $k=>$v){
				$chaptername = "";
				$content = $v;
				$content =str_replace(array("<p></p>"),"",$content);
				// $chaptername = $v;
				$content ="\r\n\r\n\t".str_replace(array("<p>","</p>"),array("","\r\n\r\n\t"),$content);
                $content = htmlspecialchars_decode($content,ENT_QUOTES );
                $content = str_replace(["&emsp;","  "], "", $content);
				echo strip_tags($content);
			}
		}
        exit();
    }

    //全库下载
    public function down_all($chapter, $platform_ids) {
        $filename ="全库文案下载.txt";
		$encoded_filename = urlencode($filename);    
		$encoded_filename = str_replace("+", "%20", $encoded_filename);  
		header("Content-Type: application/octet-stream");      
		if (preg_match("/MSIE/", $_SERVER['HTTP_USER_AGENT']) ) {
			header('Content-Disposition:  attachment; filename="' . $encoded_filename . '"');      
		} elseif (preg_match("/Firefox/", $_SERVER['HTTP_USER_AGENT'])) {      
			header('Content-Disposition: attachment; filename*="utf8' .  $filename . '"');      
		} else {      
			header('Content-Disposition: attachment; filename="' .  $filename . '"');      
		} 
		if($chapter){
			foreach ($chapter as $k=>$v){
				$chaptername = "";
				$content = $v;
				$content =str_replace(array("<p></p>"),"",$content);
				// $chaptername = $v;
                if(in_array($platform_ids , [3,4])){
                    $content = str_replace(array("<p>","</p>"),array("","\r\n\r\n\t"),$content)."@@".PHP_EOL;
                }else{
                    $content = str_replace(array("<p>","</p>"),array("","\r\n\r\n\t"),$content).PHP_EOL;
                }
                $content = str_replace("&emsp;", "", $content);
                $content = str_replace(["&emsp;","  "], "", $content);
				echo strip_tags($content);
			}
		}
        exit();
    }
}